﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebChart;

public partial class Admin_Components_Report_RevenueChart : System.Web.UI.UserControl
{
    DataTable dt = new DataTable("Chart");
    string X, Y;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            int ShopID = Session["ShopID"] == null ? 0 : int.Parse(Session["ShopID"].ToString());
            //Load tinh thanh
            int ProvinceID = Session["ProvinceID"] == null ? 0 : int.Parse(Session["ProvinceID"].ToString());
            int isProvinceAll = (ProvinceID == 0 ? 1 : 0);
            Utils.loadLocation(cboProvince, 0, isProvinceAll, ProvinceID);
            //load Shop
            //int ShopID = Session["ShopID"] == null ? 0 : int.Parse(Session["ShopID"].ToString());
            int isShopAll = (ShopID == 0 ? 1 : 0);
            Utils.loadShopByProvinceToCombo(cboShop, isShopAll, ProvinceID, ShopID, "");

            Utils.loadProductToComboSearch(cboProduct, 1, "R");
            Utils.loadLocation(cboProvince_P, 0, isProvinceAll, ProvinceID);
            Utils.loadShopByProvinceToCombo(cboShop_P, isShopAll, ProvinceID, ShopID, "");
            ChartControl1.Visible = false;
        }
    }
    protected void cboProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        int ProvinceID = int.Parse(this.cboProvince.SelectedValue == "" ? "0" : this.cboProvince.SelectedValue);
        Utils.loadShopByProvinceToCombo(cboShop, 1, ProvinceID, 0, "");
    }

    protected void btnSearchForShop_Click(object sender, EventArgs e)
    {
            ChartControl1.Visible = false;
            if (this.cboViewShopType.SelectedValue == "s")
            {
                loadDataPieChartForShop();
            }
            else if (this.cboViewShopType.SelectedValue == "m")
            {
                loadDataBarChartForShop();
            }

            if (dt.Rows.Count > 0)
            {
                lblAlert.Visible = false;
                ChartControl1.Visible = true;

                if (this.cboViewShopType.SelectedValue == "s")
                {
                    createPieChartForShop();
                }
                else if (this.cboViewShopType.SelectedValue == "m")
                {
                    createBarChartForShop();
                }
            }
            else
            {
                lblAlert.Visible = true;
                lblAlert.Text = "Không tìm thấy dữ liệu";
                ChartControl1.Visible = false;
            }
    }
    protected void btnSearchForProduct_Click(object sender, EventArgs e)
    {
        
        if (this.cboViewProductType.SelectedValue == "p")
        {
            loadDataPieChartForProduct();
        }
        else if (this.cboViewProductType.SelectedValue == "pm")
        {
            loadDataBarChartForProduct();
        }

        if (dt.Rows.Count > 0)
        {
            lblAlert.Visible = false;
            ChartControl1.Visible = true;

            if (this.cboViewProductType.SelectedValue == "p")
            {
                createPieChartForProduct();
            }
            else if (this.cboViewProductType.SelectedValue == "pm")
            {
                createBarChartForProduct();
            }
        }
        else
        {
            lblAlert.Visible = true;
            lblAlert.Text = "Không tìm thấy dữ liệu";
            ChartControl1.Visible = false;
        }
    }
    protected void btnExportExcel_Click(object sender, EventArgs e)
    {
    }
    protected void cboViewShopType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (this.cboViewShopType.SelectedValue == "s")
        {
            this.cboShop.SelectedIndex = 0;
            this.cboShop.Enabled = false;
        }
        else
        {
            this.cboShop.Enabled = true;

        }
    }

    /*
     * Chart
     */
    
    //config color
    private void ConfigureColors()
    {

        ChartControl1.Legend.Position = LegendPosition.Right;
        ChartControl1.Legend.Width = 80;
        ChartControl1.YAxisFont.ForeColor = Color.Black;
        ChartControl1.XAxisFont.ForeColor = Color.Black;
        // ChartControl1.ChartTitle.Text = "Month wise Product Distribution";
        ChartControl1.ChartTitle.ForeColor = Color.White;
        //  ChartControl1.XTitle.Text = "Month";
        //ChartControl1.YTitle.Text = "Product";

    }

    private void loadDataPieChartForShop()
    {
        DateTime FromDate;
        if (dtpFromDate.SelectedDate == null)
        {
            FromDate = DateTime.Parse("2014-01-01");
        }
        else
        {
            FromDate = dtpFromDate.SelectedDate.Value;
        }
        DateTime ToDate;
        if (dtpToDate.SelectedDate == null)
        {
            ToDate = DateTime.Parse("2099-01-01");
        }
        else
        {
            ToDate = dtpToDate.SelectedDate.Value;
        }
        int ProvinceID = int.Parse(this.cboProvince.SelectedValue == "" ? "0" : this.cboProvince.SelectedValue);
        DBDataContext db = LINQUtil.GetDBWriteOnly();
        dt = db.Report_RevenueForShopByPercent(ProvinceID, int.Parse(this.cboShop.SelectedValue), FromDate, ToDate).ToList().ToDataTable();
    }
    
    private void createPieChartForShop()
    {
        
            ChartControl1.ChartTitle.Text = "% Doanh thu theo cửa hàng";
            // ChartControl1.XTitle.Text = "Month";
            //ChartControl1.YTitle.Text = "Product";
            ChartControl1.GridLines = WebChart.GridLines.None;
            PieChart chart = new PieChart();

            chart.Fill.Color = Color.FromArgb(50, Color.SteelBlue);
            chart.Line.Color = Color.SteelBlue;
            chart.Line.Width = 1;
            chart.ShowLineMarkers = false;
            // chart.
            chart.DataLabels.Visible = true;

            X = "ShopName";
            Y = "Patient No.";

            chart.DataXValueField = X;
            chart.DataYValueField = Y;

            //looping through datatable and adding to chart control
            foreach (DataRow dr2 in dt.Rows)
            {
                chart.Data.Add(new ChartPoint(dr2["ShopName"].ToString(), (float)System.Convert.ToSingle(dr2["Per"])));
            }

            chart.ShowLegend = true;

            ConfigureColors();

            ChartControl1.Charts.Clear();
            ChartControl1.Charts.Add(chart);
            ChartControl1.HasChartLegend = true;
            ChartControl1.Legend.Width = 170;

            ChartControl1.RedrawChart();
    }

    private void createBarChartForShop()
    {
        ChartControl1.ChartTitle.Text = "Doanh thu cửa hàng theo tháng";
        ChartControl1.GridLines = WebChart.GridLines.Horizontal;
        ColumnChart chart = new ColumnChart();
        chart.Fill.Color = Color.FromArgb(50, Color.SteelBlue);
        chart.Line.Color = Color.Black;
        chart.Line.Width = 1;
        chart.ShowLineMarkers = true;
        chart.DataLabels.Visible = true;

        X = "MMYYYY";
        Y = "Patient No.";

        chart.DataXValueField = X;
        chart.DataYValueField = Y;

        //looping through datatable and adding to chart control
        foreach (DataRow dr2 in dt.Rows)
        {
            chart.Data.Add(new ChartPoint(dr2["MMYYYY"].ToString(), (int)System.Convert.ToSingle(dr2["Revenue"])));
        }
        chart.MaxColumnWidth = 40;
        
        ConfigureColors();
        ChartControl1.Charts.Clear();
        ChartControl1.Charts.Add(chart);

        ChartControl1.HasChartLegend = false;
        chart.ShowLegend = false;
        ChartControl1.RedrawChart();
    }

    private void loadDataBarChartForShop()
    {
        DateTime FromDate;
        if (dtpFromDate.SelectedDate == null)
        {
            FromDate = DateTime.Parse("2014-01-01");
        }
        else
        {
            FromDate = dtpFromDate.SelectedDate.Value;
        }
        DateTime ToDate;
        if (dtpToDate.SelectedDate == null)
        {
            ToDate = DateTime.Parse("2099-01-01");
        }
        else
        {
            ToDate = dtpToDate.SelectedDate.Value;
        }
        int ProvinceID = int.Parse(this.cboProvince.SelectedValue == "" ? "0" : this.cboProvince.SelectedValue);
        DBDataContext db = LINQUtil.GetDBWriteOnly();
        dt = db.Report_RevenueForShopByMonth(ProvinceID, int.Parse(this.cboShop.SelectedValue), FromDate, ToDate).ToList().ToDataTable();
    }

    private void loadDataPieChartForProduct()
    {
        DateTime FromDate;
        if (dtpFromDate_P.SelectedDate == null)
        {
            FromDate = DateTime.Parse("2014-01-01");
        }
        else
        {
            FromDate = dtpFromDate_P.SelectedDate.Value;
        }
        DateTime ToDate;
        if (dtpToDate_P.SelectedDate == null)
        {
            ToDate = DateTime.Parse("2099-01-01");
        }
        else
        {
            ToDate = dtpToDate_P.SelectedDate.Value;
        }
        int ProvinceID = int.Parse(this.cboProvince_P.SelectedValue == "" ? "0" : this.cboProvince_P.SelectedValue);
        int ProductID = int.Parse(this.cboProduct.SelectedValue == "" ? "0" : this.cboProduct.SelectedValue);
        DBDataContext db = LINQUtil.GetDBWriteOnly();
        dt = db.Report_RevenueForProductByPercent(ProvinceID, int.Parse(this.cboShop_P.SelectedValue), ProductID, FromDate, ToDate).ToList().ToDataTable();
    }
    private void loadDataBarChartForProduct()
    {
        DateTime FromDate;
        if (dtpFromDate_P.SelectedDate == null)
        {
            FromDate = DateTime.Parse("2014-01-01");
        }
        else
        {
            FromDate = dtpFromDate_P.SelectedDate.Value;
        }
        DateTime ToDate;
        if (dtpToDate_P.SelectedDate == null)
        {
            ToDate = DateTime.Parse("2099-01-01");
        }
        else
        {
            ToDate = dtpToDate_P.SelectedDate.Value;
        }
        int ProvinceID = int.Parse(this.cboProvince_P.SelectedValue == "" ? "0" : this.cboProvince_P.SelectedValue);
        int ProductID = int.Parse(this.cboProduct.SelectedValue == "" ? "0" : this.cboProduct.SelectedValue);
        DBDataContext db = LINQUtil.GetDBWriteOnly();
        dt = db.Report_RevenueForProductByMonth(ProvinceID, int.Parse(this.cboShop_P.SelectedValue), ProductID, FromDate, ToDate).ToList().ToDataTable();
    }
    private void createPieChartForProduct()
    {

        ChartControl1.ChartTitle.Text = "% Trọng lượng theo sản phẩm";
    
        ChartControl1.GridLines = WebChart.GridLines.None;
        PieChart chart = new PieChart();

        chart.Fill.Color = Color.FromArgb(50, Color.SteelBlue);
        chart.Line.Color = Color.SteelBlue;
        chart.Line.Width = 1;
        chart.ShowLineMarkers = false;
        // chart.
        chart.DataLabels.Visible = true;

        X = "ProductName";
        Y = "Patient No.";

        chart.DataXValueField = X;
        chart.DataYValueField = Y;

        //looping through datatable and adding to chart control
        foreach (DataRow dr2 in dt.Rows)
        {
            chart.Data.Add(new ChartPoint(dr2["ProductName"].ToString(), (float)System.Convert.ToSingle(dr2["Per"])));
        }

        chart.ShowLegend = true;

        ConfigureColors();

        ChartControl1.Charts.Clear();
        ChartControl1.Charts.Add(chart);
        ChartControl1.HasChartLegend = true;
        ChartControl1.Legend.Width = 170;

        ChartControl1.RedrawChart();
    }
    private void createBarChartForProduct()
    {
        ChartControl1.ChartTitle.Text = "Trọng lượng sản phẩm theo tháng";
        ChartControl1.GridLines = WebChart.GridLines.Horizontal;
        ColumnChart chart = new ColumnChart();
        chart.Fill.Color = Color.FromArgb(50, Color.SteelBlue);
        chart.Line.Color = Color.Black;
        chart.Line.Width = 1;
        chart.ShowLineMarkers = true;
        chart.DataLabels.Visible = true;

        X = "MMYYYY";
        Y = "Patient No.";

        chart.DataXValueField = X;
        chart.DataYValueField = Y;

        //looping through datatable and adding to chart control
        foreach (DataRow dr2 in dt.Rows)
        {
            chart.Data.Add(new ChartPoint(dr2["MMYYYY"].ToString(), (int)System.Convert.ToSingle(dr2["Weight"])));
        }
        chart.MaxColumnWidth = 40;

        ConfigureColors();
        ChartControl1.Charts.Clear();
        ChartControl1.Charts.Add(chart);

        ChartControl1.HasChartLegend = false;
        chart.ShowLegend = false;
        ChartControl1.RedrawChart();
    }

}